package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;

    @Override
    public IPage<UserBean> findByItem(int pageNO, String code) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (code != null && code.length() != 0){
            qw.like("u_code",code);
        }
        return userMapper.selectPage(new Page<UserBean>(pageNO,PAGE_SIZE),qw);
    }

    @Override
    public void add(UserBean user, int[] roleArray) {
        userMapper.insert(user);
        userMapper.add(user.getId(),roleArray);
    }

    @Override
    public void del(int userId) {
        userMapper.del(userId);
        userMapper.deleteById(userId);
    }

    @Override
    public UserBean findById(int id) {
        UserBean user = userMapper.selectById(id);
        user.setRoleList(userMapper.findByUserId(user.getId()));
        user.setGradeList(userMapper.findById(user.getId()));
        return user;
    }

    @Override
    public void updateRole(int userId, int[] roleArray) {
        userMapper.updateRole(userId,roleArray);
    }

}
